/**
 * @ClassName CODRemovalRateServiceImpl
 * @Description TODO
 * @Author 沉默小吴
 * @Date 2024/12/24 15:12
 */
package com.tledu.operate.count.service.impl;

import com.tledu.operate.count.entity.vo.CODRemovalRateJSON;
import com.tledu.operate.count.mapper.CODRemovalRateMapper;
import com.tledu.operate.count.service.ICODRemovalRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CODRemovalRateServiceImpl implements ICODRemovalRateService {
   @Autowired
   private CODRemovalRateMapper codRemovalRateMapper;
    @Override
    public Map<String, Object> getCODRemovalRates(String startDate, String endDate, String departmentId) {
        List<CODRemovalRateJSON> data = codRemovalRateMapper.selectByDateRangeAndDepartment(startDate, endDate, departmentId);

        // 提取日期标签
        List<String> labels = data.stream()
                .map(CODRemovalRateJSON::getDate)
                .distinct()
                .sorted()
                .collect(Collectors.toList());
        // 提取每个项目的数据
        Map<String, List<Double>> projectDataMap = new HashMap<>();
        for (CODRemovalRateJSON item : data) {
            projectDataMap.computeIfAbsent("一系调节池", k -> new ArrayList<>()).add(item.getOneSeriesRegulationPool());
            projectDataMap.computeIfAbsent("一系二沉", k -> new ArrayList<>()).add(item.getOneSeriesSecondarySettlement());
            projectDataMap.computeIfAbsent("二系二沉", k -> new ArrayList<>()).add(item.getTwoSeriesSecondarySettlement());
            projectDataMap.computeIfAbsent("去除率", k -> new ArrayList<>()).add(item.getRemovalRate());
        }

        // 构建最终的 JSON 结构
        Map<String, Object> result = new HashMap<>();
        result.put("labels", labels);

        List<Map<String, Object>> datasets = new ArrayList<>();
        for (Map.Entry<String, List<Double>> entry : projectDataMap.entrySet()) {
            Map<String, Object> dataset = new HashMap<>();
            dataset.put("name", entry.getKey());
            dataset.put("data", entry.getValue());
            datasets.add(dataset);
        }

        result.put("datasets", datasets);
        return result;
    }
}